﻿using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Symbology;
using DotSpatial.Topology;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KASEM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdAddShp_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Filter = "*.shp|*.shp";
            openFileDlg.ShowDialog();
            if (openFileDlg.FileName.Length > 0)
            {
                mapSource.AddLayer(openFileDlg.FileName);
                cmdAddShp.Enabled = false;
            }
        }

        private void cmdZoomIn_Click(object sender, EventArgs e)
        {
            mapSource.FunctionMode = FunctionMode.ZoomIn;
        }

        private void cmdZoomOut_Click(object sender, EventArgs e)
        {
            mapSource.FunctionMode = FunctionMode.ZoomOut;
        }

        private void cmdZoomExtend_Click(object sender, EventArgs e)
        {
            Envelope env = new Envelope();
            mapSource.ViewExtents = mapSource.Extent;
        }

        private void cmdPan_Click(object sender, EventArgs e)
        {
            mapSource.FunctionMode = FunctionMode.Pan;
        }

        private void cmdInformation_Click(object sender, EventArgs e)
        {
            mapSource.FunctionMode = FunctionMode.Info;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Filter = "*.shp|*.shp";
            openFileDlg.ShowDialog();
            if (openFileDlg.FileName.Length > 0)
            {
                mapCompare.AddLayer(openFileDlg.FileName);
                cmdAddShp.Enabled = false;
            }
        }

        private void cmdComapre_Click(object sender, EventArgs e)
        {
           
            FeatureSet LinkError = new FeatureSet();
            FeatureSet LinkComplete = new FeatureSet();
            FeatureSet FeSource = mapSource.Layers[0].DataSet as FeatureSet;
            FeatureSet Fe4Compare = mapCompare.Layers[0].DataSet as FeatureSet;
            for (int i = 0; i < FeSource.NumRows(); i++)
            {
                bool chk = false;
                IFeature iFeSource = FeSource.GetFeature(i);  
                for (int j = 0; j < Fe4Compare.NumRows(); j++)
                {
                    IFeature iFeCompare = Fe4Compare.GetFeature(j);  
                    //-----------------------
                    if (iFeSource.NumPoints == iFeCompare.NumPoints)
                    {
                        chk = true;
                        for (int ii = 0; ii < iFeSource.NumPoints; ii++)
                        {
                            double x1 = Math.Round( iFeCompare.Coordinates[ii].X,0);
                            double y1 = Math.Round(iFeCompare.Coordinates[ii].Y,0);
                            double x2 = Math.Round(iFeSource.Coordinates[ii].X,0);
                            double y2 = Math.Round(iFeSource.Coordinates[ii].Y, 0);
                            if (x1 != x2 | y1 != y2)
                            {
                                chk = false;
                            }
                        }
                        if (chk == true) break;
                    }
                    //-----------------------
                }
                if (chk == false)
                {
                    LinkError.AddFeature(iFeSource); 
                }
                else
                {
                    LinkComplete.AddFeature(iFeSource);
                }                   
            }
            MapLineLayer LinkCompleteLyr= new   MapLineLayer(LinkComplete);
            MapLineLayer LineErrorLyr= new   MapLineLayer(LinkError);

            LinkCompleteLyr.Symbolizer = new LineSymbolizer(Color.Green, 2);
            LineErrorLyr.Symbolizer = new LineSymbolizer(Color.Red, 3);

            mapCompare.Layers.Add(LinkCompleteLyr);
            mapCompare.Layers.Add(LineErrorLyr);
        }

    }
}
